/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import auxx.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author yan
 */
public class ChallengesFrame extends JFrame{


    
    Box main;
    Eventer evt;
    ArrayList<Challenge> cha;
    Challenge easyone,twentyOneShots,updown,littlebigproblem,impossible4;
    File challengesFile;
    
    public ChallengesFrame() {
        evt = new Eventer();
        cha = new ArrayList<Challenge>();
        main = Box.createVerticalBox();
        challengesFile = new File("config/challenges.dat");
        
        easyone = new Challenge("The Easy One");
        easyone.setDescription("This is the easiest challenge here, don't expect to see "
                + "any other like it again!!!"
                + "\nYou just have to survive for 30 seconds on easy!\n"
                + "And don't loose any lives or \"eat\" the green rectangles!!\n"
                + "If you don't succed, please kill yourself...");
        easyone.setDifficulty(Difficulty.EASY);
        easyone.setDoable(new Doable () {
            BallGame b = BallGame.BG;
            public void doStuff() {}

            public boolean checkStuff() {
                if(b.lives<Difficulty.EASY.lives() || b.goodShotsEaten>0)
                    b.permanentlyLost = true;
                return b.time>=30;
            }
            
        });
        
        twentyOneShots = new Challenge("21 Shots");
        twentyOneShots.setDescription("Survive for one minute "
                + "with 21 fast Shots on-screen!!\n"
                + "You have 3 lives!");
        twentyOneShots.setDifficulty(4, 500,8000,3);
        twentyOneShots.setDoable(new Doable() {
            BallGame b = BallGame.BG;
            public void doStuff() {
                b.maxxspeed = b.maxyspeed = 6;
                b.minxspeed = b.minyspeed = 5;
                b.maxshots=21;
            }

            public boolean checkStuff() {
                return b.time>=60;
            }
            
        });
        
        updown = new Challenge("Up and Down");
        updown.setDescription("Achieve 100.000 points on Medium just moving\n"
                + "your ball up and down.\n"
                + "Your ball will begin with a 20px radius and won't\n"
                + "increase at least!");
        updown.setDifficulty(1000, 10, -1, 5);
        updown.setDoable(new Doable() {
            BallGame b = BallGame.BG;
            public void doStuff() {
                b.canMoveXAxis=false;
                b.r=20;
                b.maxradius=20;
            }

            public boolean checkStuff() {
                return b.score>=100000;
            }
            
        });
        
        littlebigproblem = new Challenge("LittleBigProblem");
        littlebigproblem.setDescription("Accumulate 300 Shots on Impossible!\n"
                + "Your ball will be forever small so\n"
                + "that will help you! I hope...");
        littlebigproblem.setDifficulty(100, 122, -1, 1);
        littlebigproblem.setDoable(new Doable() {
            BallGame b = BallGame.BG;
            public void doStuff() {
                b.maxradius=5;
            }

            public boolean checkStuff() {
                return b.normalShots.size()>=300;
            }
            
        });
        
        impossible4 = new Challenge("The Impossible 4");
        impossible4.setDescription("This time you're up against only 4 shots.\n"
                + "But they're the fastest, biggest ones you've ever seen!\n"
                + "Survive for a full minute and you're good enough.");
        impossible4.setDifficulty(4,500,6000,0);
        impossible4.setDoable(new Doable() {
            BallGame b = BallGame.BG;
            public void doStuff() {
                b.shotssize=50;
                b.maxshots=4;
                b.minxspeed = b.maxxspeed = 30;
                b.minyspeed = b.maxyspeed = 20;
            }

            public boolean checkStuff() {
                return b.time>=60;
            }
            
        });
        
        
        if(!challengesFile.exists()){
            try {
                challengesFile.createNewFile();
            } catch (IOException ex) {System.out.println("Shit");}
            writeChallenges();
        }
        readChallenges();
        
        add(main);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    private class Challenge {
        private String name,description;
        private boolean completed;
        private LabelButton ch;
        private JCheckBox cb;
        private Doable doit;
        private JDialog youwin,youlose;
        private Difficulty d;
        int tts,ttri,ttgs,l;
        
        
        public Challenge(String n) {
            name = n;
            completed = false;
            
            ch = new LabelButton(n);
            ch.setActionCommand(name);
            ch.addActionListener(evt);
            cb = new JCheckBox("Done", completed);
            cb.setEnabled(false);
            
            addTo(main);
            doit= null;
            d = null;
            description = "";
            createyouwin();
            cha.add(this);
        }
        
        public String getName() {
            return name;
        }
        
        public void setDoable(Doable dd) {
            doit = dd;
        }
        
        public void setDifficulty(int ttss,int ttrii,int ttgss,int ll) {
            tts = ttss;
            ttri = ttrii;
            ttgs = ttgss;
            l = ll;
        }
        
        public void setDifficulty(Difficulty d) {
            if(d==Difficulty.CUSTOM) throw new IllegalArgumentException();
            this.d = d;
        }
        
        public void setDescription(String s){ 
            description = s;
        }
        
        private void completed() {
            completed = true;
            cb.setSelected(completed);
        }
        
        private void addTo(Box box) {
            box.add(Box.createVerticalStrut(5));
            Box b = Box.createHorizontalBox();
            b.add(Box.createHorizontalStrut(5));
            b.add(ch);
            b.add(Box.createHorizontalStrut(10));
            b.add(Box.createHorizontalGlue());
            b.add(cb);
            box.add(b);
            box.add(Box.createVerticalStrut(5));
        }
        
        private void start() {
            boolean okay = false;
            if(d==null) d = Difficulty.setCustom(tts, ttri, ttgs, l);
            System.out.println(d);
            String body = "Chalenge \"" + name + "\":\n" + description +"\nAre you ready?";
            JOptionPane opt = new javax.swing.JOptionPane(body, JOptionPane.QUESTION_MESSAGE);
            String[] options = {"Sure dude","No way man!"};
            opt.setOptions(options);
            JDialog dia = opt.createDialog("Are you up for the challenge?");
            dia.pack();
            dia.setVisible(true);
            String aux = (String) opt.getValue();
            if(aux==null || aux.equals(options[1]))
                okay = false;
            else if(aux.equals(options[0]))
                okay = true;

            if(okay) {
            if(doit!=null)
                doit.doStuff();
            
            BallGame.wf.d = d;
            BallGame.wf.difficultySet = true;
            BallGame.BG.isChallenge=true;
            BallGame.BG.checker = new Doable () {

                public void doStuff() {
                    if(doit.checkStuff()) {
                        BallGame.BG.completeChallenge();
                        BallGame.BG.done = true;
                        youwin.setSize(350, 150);
                        youwin.setVisible(true);
                        completed();
                        writeChallenges();
                        dispose();
                        BallGame.BG.restart();
                    }
                    if(BallGame.BG.permanentlyLost) {
                        BallGame.BG.done = true;
                        youlose.setSize(350, 150);
                        youlose.setVisible(true);
                        dispose();
                        BallGame.BG.restart();
                    }
                }

                public boolean checkStuff() {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
                
            };
            }
        }

        private void createyouwin() {
            JOptionPane opt = new JOptionPane("Congratulations! You just completed \n\'"
                    + name + "\'!\nYour mother should be proud of you",
                    JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_CANCEL_OPTION);
            String[] s = {"I'm awesome!1!","That was easy!"};
            opt.setOptions(s);
            youwin = opt.createDialog("You're better than I thought!!");
            
            opt = new JOptionPane("You lost, just as I predicted...\n"
                    + "You're so horrible in this game",
                    JOptionPane.WARNING_MESSAGE,JOptionPane.OK_CANCEL_OPTION);
            String[] aux = {"I'm sorry!","You cheated!!"};
            opt.setOptions(aux);
            youlose = opt.createDialog("Gosh! Can't you even win a challenge???");
        }
        
        @Override
        public String toString() {
            return name + " - " + completed;
        }
        
    }
    
    private Challenge getByName (String s) {
            Challenge c = null;    
            for(Challenge ch:cha)
                if(ch.getName().equals(s))
                    c = ch;
            return c;
        }
    
    private void writeChallenges() {
        PrintWriter out = FileManager.getWriter(challengesFile, false);
        Challenge[] aux = new Challenge[cha.size()];
        aux = cha.toArray(aux);
        for(int i=0;i<aux.length;i++)
            out.println(Extra.encrypt(aux[i].toString()));
    }
    
    private void readChallenges() {
        String[] chac = FileManager.getFullText(challengesFile);
        Extra.decryptAll(chac);
        for(int i=0;i<chac.length;i++) {
            String[] chal = chac[i].split(" - ");
            if(chal.length>=1)
                if(Boolean.parseBoolean(chal[1]))
                    getByName(chal[0]).completed();
        }
    }
        
    private class Eventer implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            Challenge c = getByName(e.getActionCommand());
            c.start();
        }


    }
}
